#include "InterOpProgressReporter.h"
#include "common/logger.h"

AMCAX_EXCHANGE_NAMESPACE_BEGIN

namespace InterOp::Reporter{

InterOpProgressReporter::InterOpProgressReporter()
{

}

InterOpProgressReporter &InterOpProgressReporter::GetInstance(){
    static InterOpProgressReporter instance;
    return instance ;
} 

void InterOpProgressReporter::ProgressCallBack (double iProgress, bool& iAbort) 
{
    int progressRange = 100;
    int percent = 0;

    double actualProgressPercent = iProgress * 100;
    int progress = static_cast<int>(actualProgressPercent);
    
    if (!_psDone || _exportStarts) 
    {
        percent = (int)((100.0*progress)/double(progressRange));
        
        if(_exportStarts){
            // LOG_INFO( "Export, Output Product Structure with Parts, Progress : {}", percent);
            if (reporterProcess_) {
                reporterProcess_(LogType::EXPORT, percent) ;
            }
        }
        else{
            // LOG_INFO( "Import, Input Product Structure, Progress : {}", percent);
            if (reporterProcess_) {
                reporterProcess_(LogType::IMPORT, percent) ;
            }
        }

    } 
    else 
    {
        
        size_t cnt = _partsDone * progressRange + progress;
        percent = (int)((100.0*cnt)/(nonMissingParts*progressRange));
        if(percent > _lastProgress ){
            // LOG_INFO( "Import, Parts of Input Product Structure, Progress : {}", percent);
            if (reporterProcess_) {
                reporterProcess_(LogType::IMPORT, percent) ;
            }
        }
        _lastProgress = percent;
        if (progress == progressRange) 
            _partsDone += 1;
    }
}

void InterOpProgressReporter::ProgressCallBackWithUserData(double iProgress, bool& iAbort, void* ipUserData_dt)
{
    UserProgressCallbackClass* pCallback = static_cast<UserProgressCallbackClass*>(ipUserData_dt);
    if(pCallback)
        pCallback->IncrementCount();

    int progressRange = 100;
    int percent = 0;
    double actualProgressPercent = iProgress * 100;
    int progress = static_cast<int>(actualProgressPercent);

    if (!_psDone || _exportStarts)
    {
        percent = (int)((100.0 * progress) / double(progressRange));
        if (_exportStarts)
            LOG_INFO("Export, Output Product Structure with Parts, Progress : {}\n", percent);
        else
            LOG_INFO("Import, Input Product Structure, Progress : {}\n", percent);
    }
    else
    {
        size_t cnt = _partsDone * progressRange + progress;
        percent = (int)((100.0 * cnt) / (nonMissingParts * progressRange));
        if (percent > _lastProgress)
            LOG_INFO("Import, Parts of Input Product Structure, Progress : {}\n", percent);
        _lastProgress = percent;
        if (progress == progressRange)
            _partsDone += 1;
    }
}

void InterOpProgressReporter::SetExportStarts(bool isStart)
{
    _exportStarts = isStart;
    if (_exportStarts)
        LOG_INFO("\n Exporting Product Structure\n");
}

void InterOpProgressReporter::Reset()
{
    _psDone = false ; 
    _numParts = 0 ;
    nonMissingParts = 0 ;
    _partsDone =0 ;
    _lastProgress = -1 ; 
    _exportStarts = false ;
}

}

AMCAX_EXCHANGE_NAMESPACE_END